package com.grasscove.namethat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.grasscove.namethat.adapters.SettingsListAdapter;

public class SettingsList extends ListActivity {

	public static final String[] COUNTDOWN_MINUTES = {
		"1 minute", "2 minutes",
		"3 minutes", "4 minutes"
	};
	
	private static final int DIALOG_COUNTDOWN_TIME = 1;

	private SharedPreferences mSettings;
	private SharedPreferences.Editor mSettingsEdit;
	private TextView mSettingsCountdownText;
	private CheckBox mCheckbox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.settings);

		mSettings = getSharedPreferences("settings", MODE_PRIVATE);
		mSettingsEdit = mSettings.edit();

		setListAdapter(new SettingsListAdapter(this, mSettings));
		registerForContextMenu(getListView());
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		switch (position) {
			case 0:
				showDialog(DIALOG_COUNTDOWN_TIME);
				break;
			case 1:
				mCheckbox = (CheckBox) v.findViewById(R.id.settings_row_checkbox);
				mCheckbox.toggle();
				mSettingsEdit.putBoolean("start_immediatly", mCheckbox.isChecked()).commit();
				break;
			case 2:
				mCheckbox = (CheckBox) v.findViewById(R.id.settings_row_checkbox);
				mCheckbox.toggle();
				mSettingsEdit.putBoolean("startup_window", mCheckbox.isChecked()).commit();
				break;
			case 3:
				mCheckbox = (CheckBox) v.findViewById(R.id.settings_row_checkbox);
				mCheckbox.toggle();
				mSettingsEdit.putBoolean("auto_team_switch", mCheckbox.isChecked()).commit();
				break;
			default:
				break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, HomeActivity.OPTION_MENU_PLAY, 1, "Play").setIcon(R.drawable.play_menu_icon);
		menu.add(0, HomeActivity.OPTION_MENU_RULES, 2, "Rules").setIcon(R.drawable.rules_menu_icon);
		//menu.add(0, HomeActivity.OPTION_MENU_ABOUT, 3, "About").setIcon(R.drawable.play_menu_icon);
		menu.add(0, HomeActivity.OPTION_MENU_SETTINGS, 4, "Settings").setIcon(R.drawable.settings_menu_icon);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent mIntent;

		switch (item.getItemId()) {
			case HomeActivity.OPTION_MENU_PLAY:
				mIntent = new Intent(this, CategoriesList.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			case HomeActivity.OPTION_MENU_RULES:
				mIntent = new Intent(this, Rules.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			case HomeActivity.OPTION_MENU_SETTINGS:
				mIntent = new Intent(this, SettingsList.class);
				mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mIntent);
				break;
			default:
				break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);

		switch (id) {
		case DIALOG_COUNTDOWN_TIME:
			mBuilder.setCancelable(true).setTitle("Select Countdown Timer")
				.setItems(COUNTDOWN_MINUTES, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						mSettings.edit().putInt("countdown_time", item).commit();

						if (mSettingsCountdownText == null) {
							mSettingsCountdownText = (TextView) findViewById(R.id.settings_countdown_text);
						}

						mSettingsCountdownText.setText(COUNTDOWN_MINUTES[item]);
					}
				}).setOnCancelListener(new DialogInterface.OnCancelListener() {
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();
					}
				});
			return mBuilder.create();
		default:
			return null;
		}
	}
}